package com.lbmb.mdps.workbench.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.domain.WarehouseReceive;
import com.lbmb.mdps.commons.domain.WarehouseVo;
import com.lbmb.mdps.commons.exception.MedicineTypeException;
import com.lbmb.mdps.commons.exception.WarehouseException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.workbench.domain.Warehouse;
import com.lbmb.mdps.workbench.service.WarehouseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 23:11
 * description
 */
@CrossOrigin
@RestController
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    @PostMapping( "/workbench/warehouse/warehousePageList.do")
    public ReturnObjectVO warehousePageList(@RequestBody WarehouseReceive warehouseReceive) throws WarehouseException {
        /*
         * 分页获取仓库列表
         * @author 林文塨
         * @date 2022/5/30 23:15
         * @param [warehouseReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        warehouseReceive.setSkipCount((warehouseReceive.getPageNo()-1)*warehouseReceive.getPageSize());

        List<Warehouse> warehouseList = warehouseService.warehousePageList(warehouseReceive);

        rov.setOthers(warehouseList);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/warehouse/getWarehouseTotal.do")
    public ReturnObjectVO getWarehouseTotal(@RequestBody WarehouseReceive warehouseReceive) throws WarehouseException {
        /*
         * 获取仓库分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Integer total = warehouseService.getWarehouseTotal(warehouseReceive);
        rov.setNumber(total);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/warehouse/addWarehouse.do")
    public ReturnObjectVO addWarehouse(@RequestBody Warehouse warehouse) throws WarehouseException {
        /*
         * 添加仓库
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        warehouse.setState("正常");
        warehouse.setCreateTime(DateTimeUtil.getSysTime());
        warehouse.setCreateBy(warehouse.getCurrentUserId());
        warehouse.setDeleteFlag("0");
        warehouse.setId(SnowFlakeIdUtil.generateId());

        warehouseService.addWarehouse(warehouse);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/warehouse/editWarehouse.do")
    public ReturnObjectVO editWarehouse(@RequestBody Warehouse warehouse) throws WarehouseException {
        /*
         * 修改仓库
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        warehouse.setEditTime(DateTimeUtil.getSysTime());
        warehouse.setEditBy(warehouse.getCurrentUserId());

        warehouseService.editWarehouse(warehouse);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/warehouse/deleteWarehouse.do")
    public ReturnObjectVO deleteEmployee(@RequestBody String [] deleteWarehouseIds) throws WarehouseException {
        /*
         * 删除仓库
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        warehouseService.deleteWarehouse(deleteWarehouseIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @GetMapping( "/workbench/warehouse/getWarehouseData.do")
    public ReturnObjectVO getMedicineTypeData() {
        /*
         * 获取全部仓库
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        List<WarehouseVo> warehouseData = warehouseService.getAllWarehouse();

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(warehouseData);

        return rov;
    }
}
